<?php

namespace LowCode\Support;

use LowCode\Abstracts\Core;
use LowCode\Traits\HasCall;

/**
 * @method static $this make(string $name, mixed $url)
 * @method $this id(string $id) 唯一标识
 * @method $this name(string $name) 接口名称
 * @method $this url(string $url) 接口请求url
 * @method $this label(string $label) 接口描述说明
 * @method $this method(string $method = 'get' | 'post' | 'put' | 'delete' | 'patch' | 'jsonp') 接口描述说明
 * @method $this settings(array $settings) 请求 设置配置
 * @method $this headers(JsExp|JsFn $headers) 请求头配置
 * @method $this mockTemplate(JsFn $mockTemplate) 模拟数据模板
 * @method $this mock(bool $bool) 是否开启模拟数据
 * @property $id
 */
class JsApi extends Core
{
    use HasCall;

    /**
     * @param string $name 接口名称
     * @param mixed $url 接口请求url
     */
    public function __construct(string $name, mixed $url)
    {
        parent::__construct([
            "id"=>autoId("jsapi"),
            "name"=>$name,
            "url"=>$url,
            "method"=>"get",
        ]);
    }

    /**
     * 发送数据类型
     * @param string $type
     * @return $this
     */
    public function type(string $type = 'form' | 'json' | 'data'): static
    {
        $settings = $this->get("settings",[]);
        $settings["type"] = $type;
        return $this->settings($settings);
    }

    /**
     * 是否注入自定义的请求头
     * @param bool $bool
     * @return $this
     */
    public function injectHeaders(bool $bool = true): static
    {
        $settings = $this->get("settings",[]);
        $settings["injectHeaders"] = $bool;
        return $this->settings($settings);
    }

    /**
     * 是否显示 loading
     * @param bool $bool
     * @return $this
     */
    public function loading(bool $bool = true): static
    {
        $settings = $this->get("settings",[]);
        $settings["loading"] = $bool;
        return $this->settings($settings);
    }

    /**
     * 显示失败提示
     * @param bool $bool
     * @return $this
     */
    public function failMessage(bool $bool = true): static
    {
        $settings = $this->get("settings",[]);
        $settings["failMessage"] = $bool;
        return $this->settings($settings);
    }


    public function __call($method, $parameters)
    {
        return $this->toCall($method, $parameters);
    }
}
